<template>
	<view>
		<my-list-item v-if="data" :item="data"></my-list-item>


		<button @click="handleClick">收藏</button>
		<button @click="handleAddCar" type="primary">加入购物车</button>

		
		<uni-popup ref="pop" type="bottom">
			<view class="" style="background-color: white">
				<input type="text" v-model="type" placeholder="请输入分类名称"/>
				<button @click="handleClick1">收藏</button>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref
	} from 'vue';
	const pop = ref()
	const type = ref('')
	const data = ref(null)
	const collectionList = ref(uni.getStorageSync('collectionList') || []);
	const cartList = ref(uni.getStorageSync('cartList') || [])
	const handleClick = () => {
		pop.value.open()

	}
	
	const handleAddCar = () => {
		cartList.value.push(data.value)
		uni.setStorageSync('cartList', cartList.value)
	}

	const handleClick1 = () => {
		const item = collectionList.value.find(v => v.type === type.value)
		if(item) {
			item.children.push(data.value)
		} else {
			collectionList.value.push({
				type: type.value,
				children: [data.value]
			})
		}
		// collectionList.value.push(data.value);
		uni.setStorageSync('collectionList', collectionList.value)
		pop.value.close()
	}
	onLoad(async options => {
		const resp = await uniCloud.database().collection('goodList')
			.doc(options._id).get({
				getOne: true
			})

		console.log(resp.result.data)

		data.value = resp.result.data

	})
</script>

<style lang="scss">

</style>